﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Decal.Adapter;
using Decal.Adapter.Wrappers;

using InspectorGadget;

namespace InspectorGadget.Monitors
{
    public class GenericMonitor : Monitor
    {
        Monitor.callback callback;

        private string m_id;
        public string MonitorID { get { return m_id; } }

        private string ID;
        private EventHandler Target;
        private EventHandler myhandler;

        private GenericMonitor() { }

        /// <summary>
        /// Add generic EventHandler monitor
        /// </summary>
        /// <param name="monitor_id">The MonitorID for this monitor.</param>
        /// <param name="target_id">The name to give to the target</param>
        /// <param name="target">The target event handler</param>
        public GenericMonitor(string monitor_id, string target_id, EventHandler target)
        {
            m_id = monitor_id;
            Target = target;
            ID = target_id;
            myhandler = new EventHandler(handler);
        }

        public void register(Monitor.callback cb)
        {
            m_callback = cb;
            Target += myhandler;
        }

        public void unregister()
        {
            Target -= myhandler;
        }

        public void handler(object sender, EventArgs e)
        {
            StringBuilder data = new StringBuilder();

            data.Append("[[CharacterFilter_ActionComplete]]").Append(Environment.NewLine);
            data.Append("Sender: ").Append(sender.GetType().FullName).Append(Environment.NewLine);
            if(this.m_inspect)
                Globals.ShallowInspect(sender, data);
            data.Append("EventArgs: ").Append(e.GetType().FullName).Append(Environment.NewLine);
            if(this.m_inspect)
                Globals.ShallowInspect(e, data);

            m_callback(this.m_id, data.ToString());
        }
    }
}
